package com.gfxy.modules.dbcenter.controller;

import com.gfxy.modules.dbcenter.service.IGfxyDataSynchronismService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: lijixin 2021/6/10 8:30 上午
 */
@Api(tags="数据同步")
@RestController
@RequestMapping("/gfxy/dataSynchronism")
@Slf4j
public class GfxyDataSynchronismController {

    @Autowired
    private IGfxyDataSynchronismService dataSynchronismService;

    @RequestMapping(value = "/department", method = RequestMethod.POST)
    @CacheEvict(value= {CacheConstant.SYS_DEPARTS_CACHE,CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries=true)
    public Result<String> department(HttpServletRequest request) {
        Result<String> result = new Result<String>();
        String username = JwtUtil.getUserNameByToken(request);
        try {
            dataSynchronismService.synchronismDepartment(username);
            //清除部门树内存
            // FindsDepartsChildrenUtil.clearSysDepartTreeList();
            // FindsDepartsChildrenUtil.clearDepartIdModel();
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result;
    }

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @CacheEvict(value= {CacheConstant.SYS_DEPARTS_CACHE,CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries=true)
    public Result<String> user(HttpServletRequest request) {
        log.debug("开始同步用户数据");
        Result<String> result = new Result<>();
        String username = JwtUtil.getUserNameByToken(request);
        try {
            dataSynchronismService.synchronismTeacher(username);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }
}
